package com.example.administrator.my2048game;
import android.content.Context;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * 卡片类
 */
public class Card extends FrameLayout {
	private TextView tv_num; // 呈现的文字
	private int num = 0; // 绑定的编号
	private int defaultBackColor = 0x338B8B00; // 设置背景色

	public Card(Context context) {
		super(context);
		tv_num = new TextView(getContext());   //显示下
		tv_num.setTextSize(32);
		tv_num.setGravity(Gravity.CENTER);
		tv_num.setBackgroundColor(0x33ffffff);
		LayoutParams lp = new LayoutParams(-1,-1);  // 创建布局，填充满整个父局容器
		lp.setMargins(15,15,0,0); // 设置每个卡片之间的边距，这里只设置了左边距、上边距，均为 15
		addView(tv_num, lp); // 将文字添加到布局(view)中
		setNum(0); // 卡片开始数值为 0
	}

	public int getNum() {
		return num;
	}

	// 将数字转换成字符串并添加到对应的 TextView 中
	public void setNum(int num) {
		this.num = num;
		tv_num.setBackgroundColor(getBackground(num));
		if (this.num <= 0) {
			tv_num.setText("");
		} else {
			tv_num.setText(num + "");
		}
	}

	// 根据数值获取颜色
	private int getBackground(int num) {
		switch (num) {
			case 0:
				defaultBackColor = 0xffCCC0B3;
				break;
			case 2:
				defaultBackColor = 0xffEEE4DA;
				break;
			case 4:
				defaultBackColor = 0xffEDE0C8;
				break;
			case 8:
				defaultBackColor = 0xffF2B179;
				break;
			case 16:
				defaultBackColor = 0xffF49563;
				break;
			case 32:
				defaultBackColor = 0xffF5794D;
				break;
			case 64:
				defaultBackColor = 0xffF55D37;
				break;
			case 128:
				defaultBackColor = 0xffEEE863;
				break;
			case 256:
				defaultBackColor = 0xffEDB04D;
				break;
			case 512:
				defaultBackColor = 0xffECB04D;
				break;
			case 1024:
				defaultBackColor = 0xffEB9437;
				break;
			case 2048:
				defaultBackColor = 0xffEA7821;
				break;
			case 4096:
				defaultBackColor = 0xff808080;
				break;
			case 8192:
				defaultBackColor = 0xff800000;
				break;
			default:
				defaultBackColor = 0xffEA7821;
				break;
		}
		return defaultBackColor;
	}

	/**
	 * 判断卡片的数字是否相同
	 */
	public boolean equals(Card o) {
		return getNum() == o.getNum();
	}
}